package com.fivestyle.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivestyle.common.lang.Result;
import com.fivestyle.common.result.R;
import com.fivestyle.entity.OrderLog;
import com.fivestyle.service.OrderLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张代钊
 * @since 2021-08-04
 */
@RestController
@RequestMapping("/onlinebook/orderLog")
public class OrderLogController extends BaseController{
    @Autowired
    private OrderLogService logService;

    //生成微信支付二维码接口
    //参数是订单号
    @ApiOperation(value = "生成支付二维码信息",notes = "参数是订单号，生成微信支付二维码接口")
    @GetMapping("createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        //返回信息，包含二维码地址，还有其他需要的信息
        Map map = logService.createNatvie(orderNo);
        System.out.println("****返回二维码map集合:"+map);
        return R.ok().data(map);
    }

    //查询订单支付状态
    //参数：订单号，根据订单号查询 支付状态
    @ApiOperation(value = "查询订单状态",notes = "根据订单号查询 支付状态，查询订单支付状态")
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        Map<String,String> map = logService.queryPayStatus(orderNo);
        System.out.println("*****查询订单状态map集合:"+map);
        if(map == null) {
            return R.error().message("支付出错了");
        }
        //如果返回map里面不为空，通过map获取订单状态
        if(map.get("trade_state").equals("SUCCESS")) {//支付成功
            //添加记录到支付表，更新订单表订单状态
            System.out.println("============外=======马上完成订单的插入=============");
            logService.updateOrdersStatus(map);
            return R.ok().message("支付成功");
        }
        return R.error().code(25000).message("支付中");

    }



    /**
     *
     * 后台管理
     */

    @Autowired OrderLogService orderLogService;

    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('web:orderlog:list')")
    public Result info(@PathVariable("id") String id) {
        OrderLog orderslog = orderLogService.getById(id);
        Assert.notNull(orderslog, "找不到该订单记录");
        return Result.succ(orderslog);
    }

    /**
     * 根据订单号查询支付记录
     * @param orderId
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('web:orderlog:list')")
    public Result list(String orderId) {
        Page<OrderLog> pageData=orderLogService.page(getPage(),new QueryWrapper<OrderLog>()
                .like(StrUtil.isNotBlank(orderId), "order_id", orderId));
        return Result.succ(pageData);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Transactional
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('web:orderlog:delete')")
    public Result delete(@RequestBody String[] ids) {
        orderLogService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }
}

